package com.application.sistercityapplication.listadapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.sistercityapplication.main.NewDashBoardPageActivity;
import com.application.sistercityapplication.main.R;

@SuppressLint("NewApi") public class GridViewImageAdapter extends BaseAdapter {

	private Activity _activity;
	private int imageWidth;
	private ViewHolder viewHolder;
	private List<Integer> _gridSize;
	public GridViewImageAdapter(Activity activity,List<Integer> gridSize,int imageWidth) {
		this._activity = activity;
		this.imageWidth = imageWidth;
		this._gridSize = gridSize;
	}

	@Override
	public int getCount() {
		return _gridSize.size();
	}

	@Override
	public Object getItem(int position) {
		return _gridSize.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int menu = _gridSize.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.each_box_layout, null);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.showCase);
			viewHolder.relative = (RelativeLayout) convertView.findViewById(R.id.clickLayout);
			viewHolder.showCase.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth,imageWidth));

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		switch (menu) {
		case 0:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_thailand));
			break;
		case 1:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_brunai));
			break;
		case 2:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_singapore));
			break;
		case 3:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_philipine));
			break;
		case 4:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_myanmar));
			break;
		case 5:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_malaysia));
			break;
		case 6:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_lao));
			break;
		case 7:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_indonesia));
			break;
		case 8:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_cambodia));
			break;
		case 9:
			viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_vietnam));
			break;
		default:
			break;
		}
		
		viewHolder.relative.setOnClickListener(new GridOnclickListener(menu));
		
		return convertView;
	}

	public class ViewHolder {
		private ImageView showCase;
		private RelativeLayout relative;
	}
	public class GridOnclickListener implements OnClickListener{
		private int _menuPosition;
		public GridOnclickListener(int menuPosition){
			this._menuPosition = menuPosition;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(_activity,NewDashBoardPageActivity.class);
			switch (_menuPosition) {
			case 0:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_thailand));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.thailand));
				i.putExtra("COUNTRY_ID", 9);
				break;
			case 1:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_brunai));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.brunei));
				i.putExtra("COUNTRY_ID", 2);
				break;
			case 2:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_singapore));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.singapore));
				i.putExtra("COUNTRY_ID", 8);
				break;
			case 3:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_philipine));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.philiphines));
				i.putExtra("COUNTRY_ID", 7);
				break;
			case 4:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_myanmar));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.burma));
				i.putExtra("COUNTRY_ID", 11);
				break;
			case 5:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_malaysia));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.malaysia));
				i.putExtra("COUNTRY_ID", 6);
				break;
			case 6:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_lao));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.laos));
				i.putExtra("COUNTRY_ID", 5);
				break;
			case 7:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_indonesia));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.indonesia));
				i.putExtra("COUNTRY_ID", 4);
				break;
			case 8:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_cambodia));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.cambodia));
				i.putExtra("COUNTRY_ID", 3);
				break;
			case 9:
				//viewHolder.showCase.setImageDrawable(_activity.getResources().getDrawable(R.drawable.new_cambodia));
				i.putExtra("MENU_CONTRY_TITLE", _activity.getResources().getString(R.string.veitnam));
				i.putExtra("COUNTRY_ID", 10);
				break;
			default:
				break;
			}
			
			_activity.startActivity(i);
		}
		
	}
}
